@ECHO OFF

REM -----------------------------------------------------------------------
REM $Id: archive.bat 21 2007-03-31 07:45:51Z beave $
REM -----------------------------------------------------------------------

SET prog=archive.bat

SET tar_path=tar
SET zip_path=zip
SET rar_path=rar
SET unzip_path=unzip
SET unrar_path=unrar
SET gzip_path=gzip
SET bzip2_path=bzip2

REM print incoming CGI variables
ECHO # CGI_HOME: %CGI_HOME%
ECHO # CGI_ARCHIVER: %CGI_ARCHIVER%
ECHO # CGI_COMPRESSOR: %CGI_COMPRESSOR%

REM change home directory if needed
IF DEFINED CGI_HOME (
	CD %CGI_HOME%
	ECHO # CD: %CD%
)

SET ACTION=%1%
SET ARCHIVE_PATH=%2%

REM print defined action and path
ECHO # ACTION: %ACTION%
ECHO # ARCHIVE_PATH: %ARCHIVE_PATH%

IF DEFINED ACTION (
	GOTO %ACTION%
)

:help
ECHO ###################################################################
ECHO Usage.
ECHO -------------------------------------------------------------------
ECHO 1. add
ECHO %prog% add ARCHIVE_PATH 'ARCHIVE_CONTENT'
ECHO ---
ECHO 2. extract
ECHO %prog% extract ARCHIVE_PATH EXTRACT_TO
ECHO ---
ECHO 3. list
ECHO %prog% list ARCHIVE_PATH
ECHO ###################################################################
GOTO end



:add
SET ARCHIVE_CONTENT=%3%
ECHO # ARCHIVE_CONTENT: %ARCHIVE_CONTENT%

IF DEFINED CGI_ARCHIVER (
	IF %CGI_ARCHIVER% == tar (
		IF EXIST %ARCHIVE_PATH% (
			%tar_path% --append --verbose --file=%ARCHIVE_PATH% %ARCHIVE_CONTENT%
		) ELSE (
			%tar_path% --create --verbose --file=%ARCHIVE_PATH% %ARCHIVE_CONTENT%
		)
	)
	IF %CGI_ARCHIVER% == zip (
		%zip_path% %ARCHIVE_PATH% %ARCHIVE_CONTENT%
	)
	IF %CGI_ARCHIVER% == rar (
		%rar_path% a -idq %ARCHIVE_PATH% %ARCHIVE_CONTENT%
	)
)

IF DEFINED CGI_COMPRESSOR (
	IF %CGI_COMPRESSOR% == gzip (
		%gzip_path% %ARCHIVE_PATH%
	)
	IF %CGI_COMPRESSOR% == gzip2 (
		%bzip2_path% %ARCHIVE_PATH%
	)
)
GOTO end



:extract
SET EXTRACT_TO=%3%
ECHO # EXTRACT_TO: %EXTRACT_TO%

mkdir %EXTRACT_TO%

IF %CGI_ARCHIVER% == tar (
	IF DEFINED CGI_COMPRESSOR (
		IF %CGI_COMPRESSOR% == bzip2 (
			IF DEFINED EXTRACT_TO (
				%tar_path% -xjf %ARCHIVE_PATH% --directory %EXTRACT_TO%
			) ELSE (
				%tar_path% -xjf %ARCHIVE_PATH%
			)
		) ELSE (
			IF DEFINED EXTRACT_TO (
				%tar_path% -xzf %ARCHIVE_PATH% --directory %EXTRACT_TO%
			) ELSE (
				%tar_path% -xzf %ARCHIVE_PATH%
			)
		)
	) ELSE (
		IF DEFINED EXTRACT_TO (
			%tar_path% -xf %ARCHIVE_PATH% --directory %EXTRACT_TO%
		) ELSE (
			%tar_path% -xf %ARCHIVE_PATH%
		)
	)
)
IF %CGI_ARCHIVER% == zip (
	IF DEFINED EXTRACT_TO (
		%unzip_path% -o %ARCHIVE_PATH% -d %EXTRACT_TO%
	) ELSE (
		%unzip_path% -o %ARCHIVE_PATH%
	)
)
IF %CGI_ARCHIVER% == rar (
	%unrar_path% e -idq -y %ARCHIVE_PATH% %EXTRACT_TO%
)
GOTO end



:list
IF %CGI_ARCHIVER% == tar (
	%tar_path% -tvf %ARCHIVE_PATH%
)
IF %CGI_ARCHIVER% == zip (
	%unzip_path% -l %ARCHIVE_PATH%
)
IF %CGI_ARCHIVER% == rar (
	%rar_path% l %ARCHIVE_PATH%
)
GOTO end


:end
